/** 
 * ================================================================================
 * 		Project		: Kompas Bola
 * 		File		: CRUDHelper.java
 * 		Description	: Helper CRUD SQLite
 * 		Called		: SplashActivity.java,MainActivity.java
 * ================================================================================
 * */
package com.kompas.bola.application.db;

import java.util.ArrayList;
import java.util.List;

import com.kompas.bola.application.model.Content;
import com.kompas.bola.application.model.ContentDetail;
import com.kompas.bola.application.utils.Constants;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class CrudHelper extends SQLiteOpenHelper implements Crud{
	/** =========================================================================== */	
	protected static final String SCRIPT_CREATE_TABLE_KOMPASBOLA = (new StringBuilder(
			" CREATE TABLE IF NOT EXISTS ")).append(Constants.TBL_BOLA).append(" ( ")
			.append(Constants.ID).append(" integer primary key autoincrement, ")
			.append(Constants.TITLE).append(" text, ")
			.append(Constants.PHOTO).append(" text, ")
			.append(Constants.THUMB).append(" text, ")
			.append(Constants.DESC).append(" text, ")
			.append(Constants.PUBDATE).append(" text, ")
			.append(Constants.GUID).append(" text, ")
			.append(Constants.KANAL).append(" text,")
			.append(Constants.SHORTLINK).append(" text,")
			.append(Constants.STATUS).append(" text);")
			.toString();
	/** =========================================================================== */
	protected static final String SCRIPT_CREATE_TABLE_KOMPASBOLA_RELATED = (new StringBuilder(
			" CREATE TABLE IF NOT EXISTS ")).append(Constants.TBL_RELATED_BOLA).append(" ( ")
			.append(Constants.ID).append(" integer primary key autoincrement, ")
			.append(Constants.GUID).append(" text, ")
			.append(Constants.TITLE_RELATED).append(" text, ")
			.append(Constants.GUID_RELATED).append(" text);")
			.toString();
	/** =========================================================================== */
	protected static final String SCRIPT_CREATE_TABLE_KOMPASBOLA_BOOKMARK = (new StringBuilder(
			" CREATE TABLE IF NOT EXISTS ")).append(Constants.TBL_BOOKMARK_BOLA).append(" ( ")
			.append(Constants.ID).append(" integer primary key autoincrement, ")
			.append(Constants.TITLE).append(" text, ")
			.append(Constants.PHOTO).append(" text, ")
			.append(Constants.THUMB).append(" text, ")
			.append(Constants.DESC).append(" text, ")
			.append(Constants.PUBDATE).append(" text, ")
			.append(Constants.GUID).append(" text, ")
			.append(Constants.KANAL).append(" text,")
			.append(Constants.SHORTLINK).append(" text,")
			.append(Constants.STATUS).append(" text);")
			.toString();
	/** =========================================================================== */	
	/*---SELECT---*/
	private static final String QUERY_SELECT	= "" +
			"SELECT * FROM " + Constants.TBL_BOLA;
	/** =========================================================================== */	
	/*---SELECT LIST MAIN---*/
	private static final String QUERY_SELECT_BOOKMARK	= "" +
			"SELECT * FROM " + Constants.TBL_BOOKMARK_BOLA;
	/** =========================================================================== */
	/*---SELECT RELATED---*/
	private static final String QUERY_SELECT_RELATED	= "" +
			"SELECT  * FROM " + Constants.TBL_RELATED_BOLA;
	/** =========================================================================== */	
	/*---DROP---*/
	private static final String QUERY_DROP	= "" +
			"DROP TABLE " + Constants.TBL_BOLA;
	/** =========================================================================== */
	/*---DROP RELATED---*/
	private static final String QUERY_DROP_RELATED	= "" +
			"DROP TABLE " + Constants.TBL_RELATED_BOLA;
	/** =========================================================================== */
	/*---DROP RELATED---*/
	private static final String QUERY_DROP_BOOKMARK	= "" +
			"DROP TABLE " + Constants.TBL_BOOKMARK_BOLA;
	/** =========================================================================== */	
	/*---UPDATE---*/
	private static final String QUERY_UPDATE = "" +
			"UPDATE " + Constants.TBL_BOLA +
			"set TITLE = ? " +
			"PHOTO = ? " +
			"THUMB = ? " +
			"DESC = ? " +
			"PUBDATE = ? " +
			"GUID = ? " +
			"KANAL = ?";
	/** =========================================================================== */	
	/*---UPDATE---*/
	private static final String QUERY_UPDATE_BY_GUID = "" +
			"UPDATE " + Constants.TBL_BOLA;
	/** =========================================================================== */	
	Context ctx;
	/** =========================================================================== */
	public CrudHelper(Context context) {
		super(context, Constants.DB_NAME, null, Constants.DB_VERSION);
		ctx 									= context;
	}
	/** =========================================================================== */
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(SCRIPT_CREATE_TABLE_KOMPASBOLA);
		db.execSQL(SCRIPT_CREATE_TABLE_KOMPASBOLA_RELATED);
		db.execSQL(SCRIPT_CREATE_TABLE_KOMPASBOLA_BOOKMARK);
	}
	/** =========================================================================== */
	public void onUpgrade(SQLiteDatabase db, int i, int j) {
		db.execSQL((new StringBuilder("DROP TABLE IF EXISTS ")).append(Constants.TBL_BOLA).toString());
		db.execSQL((new StringBuilder("DROP TABLE IF EXISTS ")).append(Constants.TBL_RELATED_BOLA).toString());
		db.execSQL((new StringBuilder("DROP TABLE IF EXISTS ")).append(Constants.TBL_BOOKMARK_BOLA).toString());
		onCreate(db);
	}
	/** =========================================================================== */
	@Override
	public void create(Content bola, String kanal) {
		SQLiteDatabase db 						= this.getWritableDatabase();
		// TODO Auto-generated method stub
		ContentValues values 					= new ContentValues();
        try { 
			values.put(Constants.TITLE, bola.getKEY_TITLE());  
			values.put(Constants.PHOTO, bola.getKEY_PHOTO_URL());  
			values.put(Constants.THUMB, bola.getKEY_THUMB_URL());  
			values.put(Constants.DESC, bola.getKEY_DESC());  
			values.put(Constants.PUBDATE, bola.getKEY_PUB_DATE());   
			values.put(Constants.GUID, bola.getKEY_GUID());  
			values.put(Constants.STATUS, Constants.BEFORE_READ);  

			if(kanal == Constants.NULL){
				values.put(Constants.KANAL, Constants.NULL); //Kanal set NULL 		
			}else{
				values.put(Constants.KANAL,kanal);		
			}
			
			db.insert(Constants.TBL_BOLA, null, values);
			db.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/** =========================================================================== */
	@Override
	public void createBookmark(Content bola) {
		SQLiteDatabase db 						= this.getWritableDatabase();
		// TODO Auto-generated method stub
		ContentValues values 					= new ContentValues();
        try { 
			values.put(Constants.TITLE, bola.getKEY_TITLE());  
			values.put(Constants.PHOTO, bola.getKEY_PHOTO_URL());  
			values.put(Constants.THUMB, bola.getKEY_THUMB_URL());  
			values.put(Constants.DESC, bola.getKEY_DESC());  
			values.put(Constants.PUBDATE, bola.getKEY_PUB_DATE());   
			values.put(Constants.GUID, bola.getKEY_GUID());  
			values.put(Constants.STATUS, Constants.BEFORE_READ);  
			
			db.insert(Constants.TBL_BOOKMARK_BOLA, null, values);
			db.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/** =========================================================================== */
	@Override
	public void createRelated(ContentDetail bolaDetail) {
		SQLiteDatabase db 						= this.getWritableDatabase();
		// TODO Auto-generated method stub
		ContentValues values 					= new ContentValues();
        try { 
			values.put(Constants.GUID, bolaDetail.getGUID());  
			values.put(Constants.TITLE_RELATED, bolaDetail.getTITLE_RELATED());    
			values.put(Constants.GUID_RELATED, bolaDetail.getGUID_RELATED());  
			
			db.insert(Constants.TBL_RELATED_BOLA, null, values);
			db.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	/** =========================================================================== */
	@Override
	public int update(Content bola) {
		SQLiteDatabase db 						= this.getWritableDatabase();
		// TODO Auto-generated method stub
		ContentValues values 					= new ContentValues();
        try {
			values.put(Constants.TITLE, bola.getKEY_TITLE());  
			values.put(Constants.PHOTO, bola.getKEY_PHOTO_URL());  
			values.put(Constants.THUMB, bola.getKEY_THUMB_URL());  
			values.put(Constants.DESC, bola.getKEY_DESC());  
			values.put(Constants.PUBDATE, bola.getKEY_PUB_DATE());  
			values.put(Constants.GUID, bola.getKEY_GUID());			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return db.update(Constants.TBL_BOLA, values, Constants.EMPTY, new String[] {Constants.EMPTY});
	}
	/** =========================================================================== */
	@Override
	public void updateContentByGuid(String guid,String url) {
		SQLiteDatabase db 						= this.getWritableDatabase();
		String updateContent					= " SET URL = "+"'"+url+"'";	
		String where							= " WHERE GUID = "+"'"+guid+"'";
		String updateQuery						= QUERY_UPDATE_BY_GUID +" "+ updateContent +" "+ where;
		db.execSQL(updateQuery);
		db.execSQL(SCRIPT_CREATE_TABLE_KOMPASBOLA);
		db.close();
	}
	/** =========================================================================== */
	@Override
	public void updateStatusByGuid(String guid, String param) {
		SQLiteDatabase db 						= this.getWritableDatabase();
		String getParam							= param != null ? param : Constants.BEFORE_READ;
		String updateContent					= " SET STATUS = "+"'"+getParam+"'";	
		String where							= " WHERE GUID = "+"'"+guid+"'";
		String updateQuery						= QUERY_UPDATE_BY_GUID +" "+ updateContent +" "+ where;
		db.execSQL(updateQuery);
		db.execSQL(SCRIPT_CREATE_TABLE_KOMPASBOLA);
		db.close();
	}
	/** =========================================================================== */
	@Override
	public void delete() {
		// TODO Auto-generated method stub
		SQLiteDatabase db 						= this.getWritableDatabase();		
		String deleteQuery						= QUERY_DROP;
		db.execSQL(deleteQuery);
		db.execSQL(SCRIPT_CREATE_TABLE_KOMPASBOLA);
		db.close();
	}
	/** =========================================================================== */
	@Override
	public void deleteRelated() {
		// TODO Auto-generated method stub
		SQLiteDatabase db 						= this.getWritableDatabase();		
		String deleteQuery						= QUERY_DROP_RELATED;
		db.execSQL(deleteQuery);
		db.execSQL(SCRIPT_CREATE_TABLE_KOMPASBOLA_RELATED);
		db.close();
	}
	/** =========================================================================== */
	@Override
	public List<ContentDetail> getDetailByGuid(String guid) {
	    SQLiteDatabase db 						= this.getReadableDatabase();
	    String checkGuid						= Constants.EMPTY;
		List<ContentDetail> listBolaDetail		= new ArrayList<ContentDetail>();
	    
	    checkGuid								= " WHERE GUID = '" +guid+ "'";

		String selectQuery						= QUERY_SELECT +" "+checkGuid;
	    Cursor cursor 							= db.rawQuery(selectQuery, null);

	    if (cursor.moveToFirst()) {
	    	do{
	    	    ContentDetail bolaDetail			= new ContentDetail();
				bolaDetail.setTITLE(cursor.getString(1));
				bolaDetail.setSOURCE((cursor.getString(2)) != null ? cursor.getString(2) : Constants.EMPTY);
				bolaDetail.setPARAGRAPH(cursor.getString(4));
				bolaDetail.setDATELINE(cursor.getString(5));
				
				listBolaDetail.add(bolaDetail);
	    	} while (cursor.moveToNext());
	    }
	    
		return listBolaDetail;
	}
	/** =========================================================================== */
	@Override
	public int getByKanal(String kanal) {
	    SQLiteDatabase db 						= this.getReadableDatabase();
	    String checkKanal						= Constants.EMPTY;
	    
	    checkKanal								= " WHERE KANAL = '" +kanal+ "'";

		String selectQuery						= QUERY_SELECT +" "+checkKanal;
		int count								= 0;
        Cursor cursor 							= db.rawQuery(selectQuery, null);
        
        count									= cursor.getCount();
        cursor.close();

        return count;
	}
	/** =========================================================================== */
	@Override
	public int getByGuid(String guid) {
	    SQLiteDatabase db 						= this.getReadableDatabase();
	    String checkGuid						= Constants.EMPTY;
	    
	    checkGuid								= " WHERE GUID = '" +guid+ "'";

		String selectQuery						= QUERY_SELECT +" "+checkGuid;
		int count								= 0;
        Cursor cursor 							= db.rawQuery(selectQuery, null);
        
        count									= cursor.getCount();
        cursor.close();

        return count;
	}
	/** =========================================================================== */
	@Override
	public int getRelatedByGuid(String guid) {
	    SQLiteDatabase db 						= this.getReadableDatabase();
	    String checkGuid						= Constants.EMPTY;
	    
	    checkGuid								= " WHERE GUID = '" +guid+ "'";

		String selectQuery						= QUERY_SELECT_RELATED +" "+checkGuid;
		int count								= 0;
        Cursor cursor 							= db.rawQuery(selectQuery, null);
        
        count									= cursor.getCount();
        cursor.close();

        return count;
	}
	/** =========================================================================== */
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		SQLiteDatabase db 						= this.getReadableDatabase();
		String selectQuery						= QUERY_SELECT;
		int count								= 0;
        Cursor cursor 							= db.rawQuery(selectQuery, null);
        count									= cursor.getCount();
        cursor.close();
        
        return count;
	}
	/** =========================================================================== */
	@Override
	public int getCountBookmark() {
		// TODO Auto-generated method stub
		SQLiteDatabase db 						= this.getReadableDatabase();
		String selectQuery						= QUERY_SELECT_BOOKMARK;
		int count								= 0;
        Cursor cursor 							= db.rawQuery(selectQuery, null);
        count									= cursor.getCount();
        cursor.close();
        
        return count;
	}
	/** =========================================================================== */
	@Override
	public int getCountList(String param) {
		// TODO Auto-generated method stub
		SQLiteDatabase db 						= this.getReadableDatabase();
	    String checkKanal						= Constants.EMPTY;
	    String getParam							= param != null ? param : Constants.EMPTY;
	    
		if(getParam.equalsIgnoreCase(Constants.EMPTY))
			checkKanal							= " WHERE KANAL IS NULL ";
		else
			checkKanal							= " WHERE KANAL = '" +param+ "'";
		
	    String selectQuery						= QUERY_SELECT +" "+checkKanal;
		int count								= 0;
        Cursor cursor 							= db.rawQuery(selectQuery, null);
        count									= cursor.getCount();
        cursor.close();
        
        return count;
	}
	/** =========================================================================== */
	@Override
	public int getCountRelated() {
		// TODO Auto-generated method stub
		SQLiteDatabase db 						= this.getReadableDatabase();
		String selectQuery						= QUERY_SELECT_RELATED;
		int count								= 0;
        Cursor cursor 							= (db.rawQuery(selectQuery, null) != null ? db.rawQuery(selectQuery, null) : null);
        
        if(cursor != null){
            count								= cursor.getCount();        	
        }
        
        cursor.close();
        
        return count;
	}
	/** =========================================================================== */
	@Override
	public String getStatusByGuid(String guid) {
		// TODO Auto-generated method stub
		SQLiteDatabase db 						= this.getReadableDatabase();
	    String checkGuid						= Constants.EMPTY;
	    
	    checkGuid								= " WHERE GUID = '" +guid+ "'";
	    
		String selectQuery						= QUERY_SELECT +" "+checkGuid;;
		String status							= Constants.EMPTY;
		Cursor cursor 							= (db.rawQuery(selectQuery, null) != null ? db.rawQuery(selectQuery, null) : null);
		
		if(cursor != null){
		    if (cursor.moveToFirst()) {
		    	do{		    	    
					status						= cursor.getString(9);
		    	} while (cursor.moveToNext());
		    }
		}

        cursor.close();
        
		return status;
	}
	/** =========================================================================== */
	@Override
	public String checkUpdateNews() {
		// TODO Auto-generated method stub
		SQLiteDatabase db 						= this.getReadableDatabase();
	    
		String selectQuery						= QUERY_SELECT +" LIMIT 1";
		String status							= Constants.EMPTY;
		Cursor cursor 							= (db.rawQuery(selectQuery, null) != null ? db.rawQuery(selectQuery, null) : null);
		
		if(cursor != null){
		    if (cursor.moveToFirst()) {
		    	do{		    	    
					status						= cursor.getString(5);
		    	} while (cursor.moveToNext());
		    }
		}

        cursor.close();
        
		return status;
	}
	/** =========================================================================== */
	@Override
	public List<Content> getAll(int page, String kanal) {
		// TODO Auto-generated method stub
		List<Content> listBola					= new ArrayList<Content>();
		int limit								= Integer.parseInt(Constants.CONTENT_LENGTH);
		String checkLimit						= Constants.EMPTY;
		String checkKanal						= Constants.EMPTY;
		
		checkLimit 								= "LIMIT "+limit+" OFFSET "+(page-1)*limit; 
		
		if(kanal == Constants.NULL){
			checkKanal							= "WHERE KANAL IS NULL";			
		}else{
			checkKanal							= "WHERE KANAL = '"+kanal+"'";				
		}
		
		String orderBy							= " ORDER BY pubDate DESC ";

		String selectQuery						= QUERY_SELECT +" "+checkKanal+" "+orderBy;//+" "+checkLimit;
	    
	    SQLiteDatabase db 						= this.getWritableDatabase();
	    Cursor cursor 							= (db.rawQuery(selectQuery, null) != null ? db.rawQuery(selectQuery, null) : null);
	    

		if(cursor != null){
		    if (cursor.moveToFirst()) {
		    	do{
			    	Content bola 				= new Content();
			    	bola.setKEY_ID(Integer.parseInt(cursor.getString(0)));
			    	bola.setKEY_TITLE(cursor.getString(1));
			    	bola.setKEY_PHOTO_URL(cursor.getString(2));
			    	bola.setKEY_THUMB_URL(cursor.getString(3));
			    	bola.setKEY_DESC(cursor.getString(4));
			    	bola.setKEY_PUB_DATE(cursor.getString(5));
			    	bola.setKEY_GUID(cursor.getString(6));
			    	
			    	listBola.add(bola);
		    	} while (cursor.moveToNext());
		    }			
		}

        cursor.close();
		return listBola;
	}
	/** =========================================================================== */
	@Override
	public List<Content> getAllBookmark(int page) {
		// TODO Auto-generated method stub
		List<Content> listBola					= new ArrayList<Content>();
		int limit								= Integer.parseInt(Constants.CONTENT_LENGTH);
		String checkLimit						= Constants.EMPTY;
		
		checkLimit 								= "LIMIT "+limit+" OFFSET "+(page-1)*limit; 		
		
		String orderBy							= " ORDER BY ID ";

		String selectQuery						= QUERY_SELECT_BOOKMARK +" "+orderBy+" "+checkLimit;
	    
	    SQLiteDatabase db 						= this.getWritableDatabase();
	    Cursor cursor 							= (db.rawQuery(selectQuery, null) != null ? db.rawQuery(selectQuery, null) : null);
	    

		if(cursor != null){
		    if (cursor.moveToFirst()) {
		    	do{
			    	Content bola 				= new Content();
			    	bola.setKEY_ID(Integer.parseInt(cursor.getString(0)));
			    	bola.setKEY_TITLE(cursor.getString(1));
			    	bola.setKEY_PHOTO_URL(cursor.getString(2));
			    	bola.setKEY_THUMB_URL(cursor.getString(3));
			    	bola.setKEY_DESC(cursor.getString(4));
			    	bola.setKEY_PUB_DATE(cursor.getString(5));
			    	bola.setKEY_GUID(cursor.getString(6));
			    	
			    	listBola.add(bola);
		    	} while (cursor.moveToNext());
		    }			
		}

        cursor.close();
		return listBola;
	}
	/** =========================================================================== */
	@Override
	public Content getDetail(int offset,String kanal) {
		// TODO Auto-generated method stub
		int limit								= Constants.DETAIL_LIMIT;
		String checkLimit						= Constants.EMPTY;
		String checkKanal						= Constants.EMPTY;

		if(kanal == Constants.NULL){
			checkKanal							= "WHERE KANAL IS NULL";			
		}else{
			checkKanal							= "WHERE KANAL = '"+kanal+"'";				
		}
		
		String orderBy							= " ORDER BY pubDate DESC ";
		
		checkLimit 								= "LIMIT "+limit+" OFFSET "+(offset-1)*limit; 

		String selectQuery						= QUERY_SELECT +" "+checkKanal+" "+orderBy+" "+checkLimit;
	    
	    SQLiteDatabase db 						= this.getWritableDatabase();
	    Cursor cursor 							= db.rawQuery(selectQuery, null);
    	Content bola 							= new Content();
	    
	    if (cursor.moveToFirst()) {
	    	do{
		    	bola.setKEY_ID(Integer.parseInt(cursor.getString(0)));
		    	bola.setKEY_TITLE(cursor.getString(1) != null ? cursor.getString(1) : Constants.EMPTY);
		    	bola.setKEY_PHOTO_URL(cursor.getString(2) != null ? cursor.getString(2) : Constants.EMPTY);
		    	bola.setKEY_THUMB_URL(cursor.getString(3) != null ? cursor.getString(3) : Constants.EMPTY);
		    	bola.setKEY_DESC(cursor.getString(4) != null ? cursor.getString(4) : Constants.EMPTY);
		    	bola.setKEY_PUB_DATE(cursor.getString(5) != null ? cursor.getString(5) : Constants.EMPTY);
		    	bola.setKEY_GUID(cursor.getString(6) != null ? cursor.getString(6) : Constants.EMPTY);		    	
		    	bola.setKEY_KANAL(cursor.getString(7) != null ? cursor.getString(7) : Constants.EMPTY);
	    	} while (cursor.moveToNext());
	    }
		return bola;
	}
	/** =========================================================================== */
	@Override
	public Content getDetailRelated(int offset, String guid) {
		// TODO Auto-generated method stub
		int limit								= Constants.DETAIL_LIMIT;
		String checkLimit						= Constants.EMPTY;
		String checkGuid						= Constants.EMPTY;

		if(guid == Constants.NULL){
			checkGuid							= "WHERE GUID IS NULL";			
		}else{
			checkGuid							= "WHERE GUID = '"+guid+"'";				
		}
		
		checkLimit 								= "LIMIT "+limit+" OFFSET "+(offset-1)*limit; 

		String selectQuery						= QUERY_SELECT_RELATED +" "+checkGuid+" "+checkLimit;
	    
	    SQLiteDatabase db 						= this.getWritableDatabase();
	    Cursor cursor 							= db.rawQuery(selectQuery, null);
    	Content bola 							= new Content();
	    
	    if (cursor.moveToFirst()) {
	    	do{
		    	bola.setKEY_ID(Integer.parseInt(cursor.getString(0)));
		    	bola.setKEY_TITLE(cursor.getString(2) != null ? cursor.getString(2) : Constants.EMPTY);
		    	bola.setKEY_GUID(cursor.getString(3) != null ? cursor.getString(3) : Constants.EMPTY);	
	    	} while (cursor.moveToNext());
	    }
		return bola;
	}
	/** =========================================================================== */
	@Override
	public Content getDetailBookmark(int offset) {
		// TODO Auto-generated method stub
		int limit								= Constants.DETAIL_LIMIT;
		String checkLimit						= Constants.EMPTY;
		
		checkLimit 								= "LIMIT "+limit+" OFFSET "+(offset-1)*limit; 

		String selectQuery						= QUERY_SELECT_BOOKMARK +" "+checkLimit;
	    
	    SQLiteDatabase db 						= this.getWritableDatabase();
	    Cursor cursor 							= db.rawQuery(selectQuery, null);
    	Content bola 							= new Content();
	    
	    if (cursor.moveToFirst()) {
	    	do{
		    	bola.setKEY_ID(Integer.parseInt(cursor.getString(0)));
		    	bola.setKEY_TITLE(cursor.getString(1) != null ? cursor.getString(1) : Constants.EMPTY);
		    	bola.setKEY_GUID(cursor.getString(6) != null ? cursor.getString(6) : Constants.EMPTY);	
	    	} while (cursor.moveToNext());
	    }
		return bola;
	}
	/** =========================================================================== */
	@Override
	public List<ContentDetail> getTitleByGuid(String guid) {
	    SQLiteDatabase db 						= this.getReadableDatabase();
	    String checkGuid						= Constants.EMPTY;
		List<ContentDetail> listBolaDetail		= new ArrayList<ContentDetail>();
	    
	    checkGuid								= " WHERE GUID = '" +guid+ "'";

		String selectQuery						= QUERY_SELECT_RELATED +" "+checkGuid;
	    Cursor cursor 							= db.rawQuery(selectQuery, null);

	    if (cursor.moveToFirst()) {
	    	do{
	    	    ContentDetail bolaDetail		= new ContentDetail();
				bolaDetail.setTITLE_RELATED((cursor.getString(2)) != null ? cursor.getString(2) : Constants.EMPTY);
				
				listBolaDetail.add(bolaDetail);
	    	} while (cursor.moveToNext());
	    }
	    
		return listBolaDetail;
	}
	/** =========================================================================== */
}
